<template>
	<view class="container">
		<u--textarea v-model="content" height="200" placeholder="这一刻的想法..." border="none"></u--textarea>
		<view class="rate flex-row row-middle">
			<text>服务态度</text>
			<u-rate :count="count" size="36" v-model="mannerVal"></u-rate>
		</view>
		<view class="rate flex-row row-middle">
			<text>专业水平</text>
			<u-rate :count="count" size="36" v-model="qualityVal"></u-rate>
		</view>
		<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="fileList" multiple
			:maxCount="maxCount" :width="240" :height="240" :accept="accept"></u-upload>
		
		<view class="footer">
			<u-button type="primary" color="#06A972" text="确定" @click="shareEval"></u-button>
		</view>
	</view>
</template>


<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	import formData from '@/utils/formData';
	export default {
		data() {
			return {
				content: '',
				maxCount: 9,
				fileList: [],
				list: [],
				fileListLen: 0,
				accept: 'image',
				mainId: 0,
				count: 5,
				mannerVal: 0,
				qualityVal: 0
			}

		},

		onLoad(option) {
			let that = this;
			that.mainId = option.mainId || 0;
			if (!that.mainId) {
				return this.$util.Tips({
					title: '商家不存在'
				}, {
					tab: 3
				});
			}
		},

		methods: {

			// 删除图片
			deletePic(event) {
				this[`${event.name}`].splice(event.index, 1)
			},
			// // 新增图片
			afterRead(event) {
				let that = this;
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				that.list = [].concat(event.file)
				that.fileListLen = that[`${event.name}`].length
				that.list.map((item) => {
					that[`${event.name}`].push({
						...item,
						status: 'success',
						message: ''
					})
				})
				console.log(that.fileList);
			},

			async shareEval() {
				let that = this,
					formdata = new formData(),
					imgUrl;

				if (!that.content) return that.$util.Tips({
					title: '请输入内容'
				});
				// if (!that.mannerVal) return that.$util.Tips({
				// 	title: '请选择服务态度'
				// })
				// if (!that.qualityVal) return that.$util.Tips({
				// 	title: '请选择专业水平'
				// })
				uni.showLoading({
					mask: true,
					title: '上传中...'
				})
				for (let i = 0; i < that.list.length; i++) {
					formdata.appendFile("evalFiles", that.list[i].url);
				}
				formdata.append("mainId", that.mainId);
				formdata.append("shareEvalType", 'CUS_INFO');
				formdata.append("evalContent", that.content);
				formdata.append("userInfoId", that.$store.state.app.uid);
				formdata.append("mannerScore", that.mannerVal);
				formdata.append("qualityScore", that.qualityVal);
				let newdata = formdata.getData();
				wx.request({
					url: HTTP_REQUEST_URL + '/api/share/eval',
					method: 'POST',
					header: {
						'content-type': newdata.contentType
					},
					data: newdata.buffer,
					success: (res) => {
						uni.hideLoading();
						if (res.data.resultCode > 0) {
							that.$util.Tips({
								title: '发布成功',
							}, {
								tab: 3
							})
						} else {
							that.$util.Tips({
								title: '上传失败',
							});
						}
					},
					fail: (res) => {
						uni.hideLoading();
						that.$util.Tips({
							title: '上传失败',
						});
					}
				});
			},
		}
	}
</script>

<style>
	.container {
		padding: 40rpx;
	}
	
	.rate {
		padding: 10rpx;
	}
	
	.rate text {
		padding-right: 10rpx;
	}

	.u-textarea {
		background-color: #f4f5f7 !important;
	}

	.u-upload {
		padding-top: 10rpx;
	}

	.u-upload__button .u-icon__icon {
		font-size: 28px !important;
	}

	.u-upload__deletable__icon .u-icon__icon {
		font-size: 20rpx !important;
		line-height: 20rpx !important;
	}

	.u-upload__success__icon .u-icon__icon {
		font-size: 24rpx !important;
		line-height: 24rpx !important;
	}
</style>